package demo01;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-04-23
 * Time: 14:27
 */
//实现一个泛型类,包含一个方法,返回数组的最大值
class Max <T extends Comparable<T> > { //此处限制了 泛型被擦除的上界,使该泛型只能接收Comparable的子类

        public T maxArrays(T[]arr) {
            T max = arr[0];
            for (int i = 0; i < arr.length; i++) {
                if( max.compareTo(arr[i]) < 0 ) {
                    max = arr[i];
                }
            }
            return max;
        }
}

class Max2  {

    public static <T  extends Comparable<T> > T maxArrays(T[]arr) {
        T max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if( max.compareTo(arr[i]) < 0 ) {
                max = arr[i];
            }
        }
        return max;
    }
}
public class Test03 {
    public static void main(String[] args) {
        Integer []array = {1,5,4,8,15,54,13};
        Integer ret = Max2.<Integer>maxArrays(array);
        System.out.println(ret);
    }
}
